from datetime import date
from typing import Optional
from fastapi_amis_admin.models.fields import Field
from fastapi_user_auth.admin import AuthAdminSite, RoleAdmin, UserAuthApp
from fastapi_user_auth.auth.models import BaseRole, BaseUser
from starlette.requests import Request
from fastapi_amis_admin.admin import AdminSite
from fastapi_amis_admin.amis.components import App
from fastapi_amis_admin_nav.admin import NavPageAdmin
from .settings import settings

# 需要安装sqlmodelx. pip install sqlmodelx
class MyUser(BaseUser, table=True):
    point: int = Field(default=0, title="积分", description="用户积分")
    phone: str = Field("", title="手机号", max_length=15)
    parent_id: Optional[int] = Field(None, title="上级", foreign_key="auth_user.id")
    birthday: Optional[date] = Field(None, title="出生日期")
    location: str = Field("", title="位置")


class MyRole(BaseRole, table=True):
    __tablename__ = "auth_role"  # 数据库表名,必须是这个才能覆盖默认模型
    icon: str = Field("", title="图标")
    is_active: bool = Field(default=True, title="是否激活")


class MyRoleAdmin(RoleAdmin):
    model = MyRole


# class MyUserRoleLink(UserRoleLink, CreateTimeMixin, table=True):
#     # 重写UserRoleLink模型, 并且创建id虚拟主键字段
#     id: str = Field(
#         None, title="虚拟主键",
#         sa_column=column_property(
#             # 注意这里的cast, 用于将联合主键转换为字符串.不要使用format, 会导致直接识别为字符串.
#             cast(UserRoleLink.user_id, String) + "-" + cast(UserRoleLink.role_id, String)
#         )
#     )
#
#     description: str = Field(None, title="描述")

from apps.audit.admin import AuditLogAdmin

class MyUserAuthApp(UserAuthApp):
    unique_id = "Auth"
    from .admin import MyUserLoginFormAdmin, MyUserRegFormAdmin
    UserLoginFormAdmin = MyUserLoginFormAdmin
    UserRegFormAdmin = MyUserRegFormAdmin
    RoleAdmin = MyRoleAdmin
    def __init__(self, app: "AdminApp"):
            super().__init__(app)
            self.register_admin(AuditLogAdmin)
            self.register_admin(NavPageAdmin)

class MyAuthAdminSite(AuthAdminSite):
    unique_id = "Auth>MyAuthAdminSite"
    UserAuthApp = MyUserAuthApp
    # 自定义应用模板,复制原模板文件修改,原路径: fastapi_amis_admin/amis/templates/app.html,
    # 注意有些ide,会把模板的变更自动前后加空格，这样会出错或成空白页。
    # 还有一个theme参数,可以设置 amis 主题, 参考: https://baidu.gitee.io/amis/zh-CN/docs/themes ,
    # ${theme_css}会使用cdn，如果要本地，需要手动更改。
    #使用patchoffline 改变page与app的模板目录及文件名，结合.env使用。,这里可以不用再指定了。
    # template_name = str(settings.TEMPLATE_PATH / 'amis_app.html')

    async def get_page(self, request: Request) -> App:
        app = await super().get_page(request)
        from fastapi_amis_admin import amis
        self.tabs_mode = amis.TabsModeEnum.vertical
        # app.amis_html = amis.Html(template_path="templates/amis_app.html",
        # )
        app.footer = (
            '<div class="p-2 text-center bg-light">Copyright © 2025 - 2025  '
            f'<a href="http://ooo.dgpms.cn:6789/admin" target="_blank" '
            'class="link-secondary">公诚管理咨询有限公司</a>. All rights reserved. '
            f'<a target="_blank" href="http://ooo.dgpms.cn:6789/admin" '
            f'class="link-secondary" rel="noopener">v0.8.0</a></div> '
        )
        return app